package cn.iocoder.yudao.hires.controller.admin;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.hires.entity.ResumeMainDO;
import cn.iocoder.yudao.hires.service.ResumeMainService;
import cn.iocoder.yudao.hires.vo.ResumeMainPageReqVO;
import cn.iocoder.yudao.hires.vo.ResumeMainSaveReqVO;
import cn.iocoder.yudao.hires.vo.ResumeOpusRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 简历-主体")
@RestController
@RequestMapping("/resume/main")
@Validated
public class ResumeMainController {


    @Resource
    private ResumeMainService mainService;


    @PostMapping("/create")
    @Operation(summary = "创建简历-主体")
    //@PreAuthorize("@ss.hasPermission('resume:main:create')")
    public CommonResult<Integer> createMain(@Valid @RequestBody ResumeMainSaveReqVO createReqVO) {
        return success(mainService.createMain(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新简历-主体")
    //@PreAuthorize("@ss.hasPermission('resume:main:update')")
    public CommonResult<Boolean> updateMain(@Valid @RequestBody ResumeMainSaveReqVO updateReqVO) {
        mainService.updateMain(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除简历-主体")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('resume:main:delete')")
    public CommonResult<Boolean> deleteMain(@RequestParam("id") Integer id) {
        mainService.deleteMain(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得简历-主体")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('resume:main:query')")
    public CommonResult<ResumeOpusRespVO> getMain(@RequestParam("id") Integer id) {
        ResumeMainDO main = mainService.getMain(id);
        return success(BeanUtils.toBean(main, ResumeOpusRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得简历-主体分页")
    //@PreAuthorize("@ss.hasPermission('resume:main:query')")
    public CommonResult<PageResult<ResumeOpusRespVO>> getMainPage(@Valid ResumeMainPageReqVO pageReqVO) {
        PageResult<ResumeMainDO> pageResult = mainService.getMainPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResumeOpusRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出简历-主体 Excel")
    //@PreAuthorize("@ss.hasPermission('resume:main:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMainExcel(@Valid ResumeMainPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResumeMainDO> list = mainService.getMainPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "简历-主体.xls", "数据", ResumeOpusRespVO.class, BeanUtils.toBean(list, ResumeOpusRespVO.class));
    }

    @GetMapping("/getDetail")
    @Operation(summary = "获得简历-主体")
    @TenantIgnore
    public CommonResult<Map<String, Object>> getDetail(@RequestParam("userId") Integer userId, @RequestParam(value = "intentionPost", required = false) String intentionPost,
                                                       @RequestParam(value = "tenantId", required = false) Integer tenantId) {
        Map<String, Object> main = mainService.getDetail(userId, intentionPost,tenantId);
        return success(main);
    }


    @GetMapping("/resumeLibrary")
    @Operation(summary = "简历库")
    @TenantIgnore
    public CommonResult<List<Map<String, Object>>> resumeLibrary(Integer categoryId) {
        List<Map<String, Object>> main = mainService.resumeLibrary(categoryId);
        return success(main);
    }


}
